//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.Provenance.Controls.View
{
    using System;
    using System.Windows.Controls;
    using Microsoft.Research.Provenance.Controls.Presenter;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using Microsoft.Research.ScientificWorkflow.UIDesigner.Monitor;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Diagnostics;
    using System.IO;

    /// <summary>
    /// Interaction logic for ActivityDataProductView.xaml
    /// </summary>
    public partial class ActivityDataProductView : UserControl
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        public ActivityDataProductView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the ActivityDataProductPresenter Model to the view's data context.
        /// </summary>
        public ActivityDataProductPresenter Model
        {
            get { return this.DataContext as ActivityDataProductPresenter; }
            set { this.DataContext = value; }
        }
        
        /// <summary>
        /// Fired on link click.
        /// </summary>
        /// <param name="sender">
        /// Link.
        /// </param>
        /// <param name="e">
        /// MouseButtonEventArgs.
        /// </param>
        private void OnLinkClick(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement link = sender as FrameworkElement;
            if (link != null)
            {
                DataProductItem dataproductView = ((ContentPresenter)(link.TemplatedParent)).DataContext as DataProductItem;
                if (dataproductView != null)
                {
                    TextBlock linkTxtBlk = link as TextBlock;
                    if (linkTxtBlk != null)
                    {
                        linkTxtBlk.Foreground = Brushes.BlueViolet;
                    }

                    try
                    {
                        Process.Start(dataproductView.HtmlPath);
                    }
                    catch (IOException ex)
                    {
                        TridentErrorHandler.HandleUIException(ex);
                    }
                    catch (System.ComponentModel.Win32Exception ex)
                    {
                        TridentErrorHandler.HandleUIException(ex);
                    }
                    catch (ObjectDisposedException ex)
                    {
                        TridentErrorHandler.HandleUIException(ex);
                    }
                    catch (Exception ex)
                    {
                        TridentErrorHandler.HandleUIException(ex);
                    }
                }
            }
        }
    }
}
